<template>
  <el-dialog v-model="visible" :title="!dataForm.sectionId ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
<!--          <el-form-item label="采购项目编号" prop="purchaseProjectId">-->
<!--        <el-input v-model="dataForm.purchaseProjectCode" placeholder="采购项目编号"></el-input>-->
<!--      </el-form-item>-->
          <el-form-item label="标包名称" prop="sectionName">
        <el-input v-model="dataForm.sectionName" placeholder="标包名称"></el-input>
      </el-form-item>
          <el-form-item label="标包编号" prop="sectionCode">
        <el-input v-model="dataForm.sectionCode" placeholder="标包编号"></el-input>
      </el-form-item>
          <el-form-item label="标包内容" prop="sectionContent">
        <el-input v-model="dataForm.sectionContent" placeholder="标包内容"></el-input>
      </el-form-item>
          <el-form-item label="标包类别" prop="sectionCategory">
            <ren-radio-group v-model="dataForm.sectionCategory" dict-type="purchase_project_type"></ren-radio-group>
          </el-form-item>
          <el-form-item label="概算金额(元)" prop="estimatesMoney">
            <el-input  v-model="dataForm.estimatesMoney" :min="0" type="number" :step="1" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="是否要求上传资质" prop="isAptitude">
            <ren-radio-group v-model="dataForm.isAptitude" dict-type="is_or_no"></ren-radio-group>
      </el-form-item>
<!--          <el-form-item label="标段状态" prop="dealStatus">-->
<!--            <ren-radio-group v-model="dataForm.dealStatus" dict-type="deal_status"></ren-radio-group>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="成交金额(元)" prop="dealMoney">-->
<!--        <el-input v-model="dataForm.dealMoney" placeholder="成交金额"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="成交企业共享平台" prop="dealBodyShareId">-->
<!--        <el-input v-model="dataForm.dealBodyShareId" placeholder="成交企业共享平台"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="成交企业名称" prop="dealBodyName">-->
<!--        <el-input v-model="dataForm.dealBodyName" placeholder="成交企业名称"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="成交时间" prop="dealTime">-->
<!--            <el-date-picker v-model="dataForm.dealTime" type="dealTime" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" placeholder="奖补申请开始时间"></el-date-picker>-->
<!--&lt;!&ndash;            <el-input v-model="dataForm.dealTime" placeholder="成交时间，格式：YYYY-MM-DD HH:MM:SS"></el-input>&ndash;&gt;-->
<!--      </el-form-item>-->
<!--          <el-form-item label="项目创建人" prop="createUser">-->
<!--        <el-input v-model="dataForm.createUser" placeholder="项目创建人"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="项目创建时间" prop="createTime">-->
<!--        <el-input v-model="dataForm.createTime" placeholder="项目创建时间，格式：YYYY-MM-DD HH:MM:SS"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="最后更新人" prop="updateUser">-->
<!--        <el-input v-model="dataForm.updateUser" placeholder="最后更新人"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="最后更新时间，格式：YYYY-MM-DD HH:MM:SS" prop="updateTime">-->
<!--        <el-input v-model="dataForm.updateTime" placeholder="最后更新时间，格式：YYYY-MM-DD HH:MM:SS"></el-input>-->
<!--      </el-form-item>-->
      </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  sectionId: '',  purchaseProjectId: '', purchaseProjectCode: '',  sectionName: '',  sectionCode: '',  sectionContent: '',  sectionCategory: '',  estimatesMoney: '',  isAptitude: '',  dealStatus: '',  dealMoney: '',  dealBodyShareId: '',  dealBodyName: '',  dealTime: '',  createUser: '',  createTime: '',  updateUser: '',  updateTime: ''});

const rules = ref({
          purchaseProjectId: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          sectionName: [
      { required: true, message: '标包名称不能为空', trigger: 'blur' }
    ],
          sectionCode: [
      { required: true, message: '标包编号不能为空', trigger: 'blur' }
    ],
          sectionContent: [
      { required: true, message: '标包内容不能为空', trigger: 'blur' }
    ],
          sectionCategory: [
      { required: true, message: '标包类别不能为空', trigger: 'blur' }
    ],是否要求上传资质: [
      { required: true, message: '标包类别不能为空', trigger: 'blur' }
    ],
          estimatesMoney: [
    { required: true,
      trigger: "blur",
      pattern: /^[0-9]+(.[0-9]{1,2})?$/, //大于0保留两位
      message: "输入的值必须为正数且保留两位小数"
    },
    { required: true,
      trigger: "blur",
      pattern: /^\d{1,9}$|^\d{1,9}[.]\d{1,2}$/, //大于0保留两位
      message: "输入的值最大为999999999.99且最多两位小数"
    }
    ]
  });

const init = (sectionId?: number) => {
  visible.value = true;
  dataForm.sectionId = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (sectionId) {
    getInfo(sectionId);
  }
};

// 获取信息
const getInfo = (sectionId: number) => {
  baseService.get("/section/section/" + sectionId).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.sectionId ? baseService.post : baseService.put)("/section/section", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init,
  dataForm
});
</script>
